<template>
  <div class="main_box" style="padding:10px">
    <el-card class="header-card">
      <div slot="header">
        <div class="header">
          <i @click="returnList" class="el-icon-arrow-left"></i>
          <div class="separater">|</div>
          <div class="title">{{ dataInfo.equipName }}</div>
        </div>
      </div>
      <el-row>
        <el-col class="right-info1">
          <el-row class="header-info">
            <el-col class="header-img__wrapper">
              <div>
                <img title="点击下载图片" @click="downloadByBlob(dataInfo.wxAppUrl, '接收器二维码')" :src="dataInfo.wxAppUrl"
                  width="170px" style="cursor:pointer" />
              </div>
            </el-col>
            <el-col class="text-info">
              <el-col>
                <el-col>接收器编号:</el-col>
                <el-col>{{ dataInfo.equipCode }}</el-col>
              </el-col>
              <el-col>
                <el-col>接收器名称:</el-col>
                <el-col>{{ dataInfo.equipName }}</el-col>
              </el-col>
              <el-col>
                <el-col>MAC地址:</el-col>
                <el-col>{{ dataInfo.equipMac }}</el-col>
              </el-col>
              <el-col>
                <el-col>设备状态:</el-col>
                <el-col>{{ formatReceiverState(dataInfo.equipState) }}</el-col>
              </el-col>
              <el-col>
                <el-col>异常状态:</el-col>
                <el-col> <span style="color:red" v-if="dataInfo.errorState == 1">异常警告</span></el-col>
              </el-col>
              <el-col>
                <el-col>创建人:</el-col>
                <el-col>{{ dataInfo.createUserName }}</el-col>
              </el-col>
              <el-col>
                <el-col>创建时间:</el-col>
                <el-col>{{ dataInfo.createDate }}</el-col>
              </el-col>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

    <el-card class="body-card">
      <el-tabs v-model="activeName" @tab-click="tabClick">
        <el-tab-pane label="基本信息" name="userInfo">
          <el-form>
            <el-row>
              <el-form-item label="修改人:">
                <span>{{ dataInfo.modifyUserName }}</span>
              </el-form-item>
              <el-form-item label="修改时间:">
                <span>{{ dataInfo.modifyDate }}</span>
              </el-form-item>
              <el-form-item label="备注:">
                <span>{{ dataInfo.remark }}</span>
              </el-form-item>
              <el-form-item label="商户:">
                <span>{{ dataInfo.businessName }}</span>
              </el-form-item>
              <el-form-item label="部门:">
                <span>{{ dataInfo.departName }} {{ dataInfo.firstDepartName }} {{ dataInfo.secondDepartName }} {{ dataInfo.thirdDepartName }}</span>
              </el-form-item>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="心跳记录" name="heartBeat">
          <heartbearRecord :typeName="activeName" :equipId="dataInfo.equipId"></heartbearRecord>
        </el-tab-pane>
        <el-tab-pane label="接收记录" name="alarm">
          <alarmRecord :typeName="activeName" :equipId="dataInfo.equipId"></alarmRecord>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import ReceiverApi from '@/api/equip/receiver';
import alarmRecord from "@/components/AlarmRecord.vue";
import heartbearRecord from "@/components/HeartbeatRecord.vue";

export default {
  data() {
    return {
      dataInfo: {},
      equipLogs: [],
      activeName: this.$route.query.hasOwnProperty("recordType")
        ? this.$route.query.recordType
        : "userInfo"
    };
  },
  components: { alarmRecord, heartbearRecord },
  computed: {},
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      ReceiverApi.getReceiverInfo({ id: this.$route.query.id }).then(res => {
        this.dataInfo = res.data
      })
    },
    tabClick(tab, event) {
      this.activeName = tab.name;
    },
    download(href, name) {
      let eleLink = document.createElement("a");
      eleLink.download = name;
      eleLink.href = href;
      eleLink.click();
      eleLink.remove();
    },
    downloadByBlob(url, name) {
      let image = new Image();
      image.setAttribute("crossOrigin", "anonymous");
      image.src = url;
      image.onload = () => {
        let canvas = document.createElement("canvas");
        canvas.width = image.width;
        canvas.height = image.height;
        let ctx = canvas.getContext("2d");
        ctx.drawImage(image, 0, 0, image.width, image.height);
        canvas.toBlob(blob => {
          let url = URL.createObjectURL(blob);
          this.download(url, name);
          // 用完释放URL对象
          URL.revokeObjectURL(url);
        });
      };
    },
    formatReceiverState(cellValue) {
      if (cellValue === 1) {
        return '待连接'
      } else if (cellValue === 2) {
        return '在线'
      } else if (cellValue === 3) {
        return '离线'
      }
    },
    returnList() {
      this.$store.dispatch('delView', this.$route);
      this.$router.push({ path: '/equip/receiverManagement' })
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/detailType.scss"
</style>

<style lang="scss" >
@import "@/assets/css/views/content.scss"
</style>
